package com.car.service.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.order.OrderDao;
import com.car.entity.Order;
import com.car.util.SnowFlakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OrderServiceImpl
 * @Description:
 * @Author 余全盛
 * @Date: 2021/12/2 13:56
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SnowFlakeIdWorker snowFlakeIdWorker;

    @Override
    public int creatOrder(Order order) {
        order.setOrderId(String.valueOf(snowFlakeIdWorker.nextId()));
        int result = orderDao.insert(order);
        return result;
    }

    @Override
    public List<Order> getOrders(Map<String, Object> param) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Order::getOrderStartDate);
        IPage<Order> page= (IPage<Order>) param.get("page");
        String orderId = (String) param.get("orderId");
        String customerId = (String) param.get("customerId");
        if (orderId!=null&& !"".equals(orderId)){
            wrapper.eq(Order::getOrderId,orderId);
        }
        if (customerId!=null&& !"".equals(customerId)){
            wrapper.eq(Order::getCustomerId,customerId);
        }
            return orderDao.selectPage(page,wrapper).getRecords();

    }

    @Override
    public int updateOrderState(String orderId,int orderState) {
        LambdaUpdateWrapper<Order> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(Order::getOrderId,orderId);//where orderId = #{orderID}
        wrapper.set(Order::getOrderState,orderState);//set orderState = #{orderState}
        return orderDao.update(null,wrapper);
    }

    @Override
    public int updateOrderInfo(String orderId, int orderState, Double orderEndCarOilValue, Double orderEndCarElectriclValue, Date orderEndDate) {
        LambdaUpdateWrapper<Order> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(Order::getOrderId,orderId);
        wrapper.set(Order::getOrderState,orderState);
        wrapper.set(Order::getOrderEndCarOilValue,orderEndCarOilValue);
        wrapper.set(Order::getOrderEndCarElectriclValue,orderEndCarElectriclValue);
        wrapper.set(Order::getOrderEndDate,orderEndDate);
        return orderDao.update(null,wrapper);
    }

    @Override
    public Order selectById(String orderId) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>();
        wrapper.eq(Order::getOrderId,orderId);
        return orderDao.selectOne(wrapper);
    }

    @Override
    public int updataOrder(Order order) {
        LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<Order>();
        wrapper.eq(Order::getOrderId,order.getOrderId());
        wrapper.set(Order::getOrderState,order.getOrderState());
        wrapper.set(Order::getOrderDeposit,order.getOrderDeposit());
        wrapper.set(Order::getOrderRentPrice,order.getOrderRentPrice());
        return orderDao.update(null,wrapper);
    }

    @Override
    public int updataOrderDepositState(Order order) {
        LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<Order>();
        wrapper.eq(Order::getOrderId,order.getOrderId());
        wrapper.set(Order::getOrderDepositState,order.getOrderDepositState());
        return orderDao.update(null,wrapper);
    }


}
